//package com.lagou.java;
//
//import org.apache.zookeeper.*;
//import org.apache.zookeeper.data.ACL;
//import org.apache.zookeeper.data.Stat;
//
//import java.util.List;
//import java.util.concurrent.TimeUnit;
//
///**
// * @Classname ZkUtils
// * @Description TODO
// * @Date 2020/8/3 12:31
// * @Created by pc
// */
//public class ZkUtils implements Watcher {
//
//    private static ZooKeeper zooKeeper = null;
//
//    private static String address = "127.0.0.1:2181";
//
//    private static int sessionTimeout = 3000;
//
//    private static int tryInterval = 60;
//
//    private volatile static WatchedEvent watchedEvent;
//
//    public static void main(String[] args) {
//        System.out.println("start ... ");
//        zooKeeper = getConnetion();
//        System.out.println(zooKeeper.getState());
//        System.out.println(address + sessionTimeout);
//    }
//
//    // 获取连接
//    private static ZooKeeper getConnetion () {
//        System.out.println(address + sessionTimeout);
//        return zooKeeper == null ? flushConnetion(address,sessionTimeout, tryInterval) : zooKeeper;
//    }
//
//    // 刷新连接
//    private static ZooKeeper flushConnetion (String address, int sessionTimeout, int tryInterval) {
//        ZooKeeper zooKeeper = null;
//        try {
//            zooKeeper = new ZooKeeper(address, sessionTimeout, new ZkUtils());
//            int count = tryInterval <= 0 ? ZkUtils.tryInterval : tryInterval;
//            while (!zooKeeper.getState().isConnected() && count > 0){
//                count--;
//                System.out.println("try connecting to ... ");
//                System.out.println(zooKeeper.getState());
//                TimeUnit.SECONDS.sleep(1);
//            }
//            if (zooKeeper != null && !zooKeeper.getState().isConnected()) {
//                throw new RuntimeException("连接超时时间:" + tryInterval + "秒已用完, 仍无法连接:" + address);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return zooKeeper;
//    }
//
//    // 异步监听事件
//    @Override
//    public synchronized void process(WatchedEvent watchedEvent) {
//        ZkUtils.watchedEvent = watchedEvent;
//    }
//
//    // 创建节点
//    public static Boolean createNode (String path, String data, List<ACL> acls, CreateMode createMode) {
//        boolean flag = false;
//        try {
//            ZooKeeper connetion = getConnetion();
//            Stat exists = connetion.exists(path, false);
//            if (exists == null) {
//                String res = connetion.create(path, data.getBytes(), acls, createMode);
//            }
//            flag = true;
//        } catch (KeeperException e) {
//            e.printStackTrace();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        return flag;
//    }
//
//    /**
//     * 创建永久节点
//     * @param path
//     * @param data
//     * @return
//     */
//    public static Boolean createPNode (String path, String data) {
//        Boolean isCreated = createNode(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//        System.out.println("==> zk 永久节点:" + path + ", 创建" + (isCreated ? "成功" : "失败"));
//        return isCreated;
//    }
//
//    /**
//     * 创建永久节点
//     * @param path
//     * @param data
//     * @return
//     */
//    public static Boolean createPSNode (String path, String data) {
//        Boolean isCreated = createNode(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
//        System.out.println("==> zk 永久顺序节点:" + path + ", 创建" + (isCreated ? "成功" : "失败"));
//        return isCreated;
//    }
//
//    // 创建临时节点
//    public static Boolean createENode (String path, String data) {
//        Boolean isCreated = createNode(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
//        System.out.println("==> zk 临时节点:" + path + ", 创建" + (isCreated ? "成功" : "失败"));
//        return isCreated;
//    }
//
//    // 创建临时节点
//    public static Boolean createESNode (String path, String data) {
//        Boolean isCreated = createNode(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
//        System.out.println("==> zk 临时顺序节点:" + path + ", 创建" + (isCreated ? "成功" : "失败"));
//        return isCreated;
//    }
//
//
//}
